#include "QQClientView.h"

Q_TAKEOVER_NATIVEEVENT_CPP(QQClientView, QQClientView::mAppBar);
QQClientView::QQClientView(QWidget* parent)
	: QMainWindow(parent)
{
	this->resize(1020, 680);
	mAppBar = new ElaAppBar(this);

	mNavigationBar = new QQNavigationBar(this);

	mCenterStackedWidget = new QQCentralStackedWidget(this);
	mCenterStackedWidget->setContentsMargins(0, 0, 0, 0);
	QWidget* centralWidget = new QWidget(this);
	QHBoxLayout* layout = new QHBoxLayout(centralWidget);
	layout->setContentsMargins(10, 0, 0, 0);
	layout->setSpacing(0);
	layout->addWidget(mNavigationBar);
	layout->addWidget(mCenterStackedWidget);

	this->setCentralWidget(centralWidget);
	connect(mAppBar, &ElaAppBar::closeButtonClicked, this, &QQClientView::closeButtonClicked);
	connect(mNavigationBar, &QQNavigationBar::userInfoCardClicked, this, &QQClientView::userInfoCardClicked);
	connect(mNavigationBar, &QQNavigationBar::navigationNodeClicked, this, &QQClientView::onNavigationNodeClicked);
	connect(mNavigationBar, &QQNavigationBar::navigationNodeClicked, this, &QQClientView::navigationNodeClicked);
}

QQClientView::~QQClientView()
{
}

void QQClientView::setIsStayTop(bool isStayTop)
{
	mAppBar->setIsStayTop(isStayTop);
}

bool QQClientView::getIsStayTop() const
{
	return mAppBar->getIsStayTop();
}

void QQClientView::setIsFixedSize(bool isFixedSize)
{
	mAppBar->setIsFixedSize(isFixedSize);
}

bool QQClientView::getIsFixedSize() const
{
	return mAppBar->getIsFixedSize();
}

void QQClientView::setIsDefaultClosed(bool isDefaultClosed)
{
	mAppBar->setIsDefaultClosed(isDefaultClosed);
}

bool QQClientView::getIsDefaultClosed() const
{
	return mAppBar->getIsDefaultClosed();
}

void QQClientView::setAppBarHeight(int appBarHeight)
{
	mAppBar->setAppBarHeight(appBarHeight);
}

int QQClientView::getAppBarHeight() const
{
	return mAppBar->getAppBarHeight();
}

void QQClientView::setIsCentralStackedWidgetTransparent(bool isTransparent)
{
	mCenterStackedWidget->setIsTransparent(isTransparent);
}

bool QQClientView::getIsCentralStackedWidgetTransparent() const
{
	return mCenterStackedWidget->getIsTransparent();
}

void QQClientView::setBaseImage(const QImage& image)
{
	mBaseImage = image;
}

QImage QQClientView::getBaseImage() const
{
	return mBaseImage;
}

void QQClientView::moveToCenter()
{
	if (this->isMaximized() || this->isFullScreen())
	{
		return;
	}
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
	auto geometry = this->screen()->availableGeometry();
#else
	auto geometry = qApp->screenAt(this->geometry().center())->geometry();
#endif
	this->setGeometry(
		(geometry.left() + geometry.right() - this->width()) / 2,
		(geometry.top() + geometry.bottom() - this->height()) / 2,
		this->width(), this->height());
}

void QQClientView::setUserInfoCardVisible(bool isVisible)
{
	mNavigationBar->setUserInfoCardVisible(isVisible);
}

void QQClientView::setUserInfoCardPixmap(QPixmap pixmap)
{
	mNavigationBar->setUserInfoCardPixmap(pixmap);
}

void QQClientView::setUserInfoCardSize(QSize size)
{
	mNavigationBar->setUserInfoCardSize(size);
}

void QQClientView::setNavigationIconSize(QSize size)
{
	mNavigationBar->setNavigationIconSize(size);
}

void QQClientView::setFooterIconSize(QSize size)
{
	mNavigationBar->setFooterIconSize(size);
}

QString QQClientView::addPageNode(QWidget* page, QIcon icon)
{
	QString nodeKey = mNavigationBar->addPageNode(page, icon);
	int nodeIndex = mCenterStackedWidget->addWidget(page);
	mRouteMap.insert(nodeKey, nodeIndex);
	return nodeKey;
}

QString QQClientView::addPageNode(QWidget* page, int keyPoints, QIcon icon)
{
	QString nodeKey = mNavigationBar->addPageNode(page, keyPoints, icon);
	int nodeIndex = mCenterStackedWidget->addWidget(page);
	mRouteMap.insert(nodeKey, nodeIndex);
	return nodeKey;
}

QString QQClientView::addFooterNode(int keyPoints, QIcon icon)
{
	QString nodeKey = mNavigationBar->addFooterNode(keyPoints, icon);
	int nodeIndex = mCenterStackedWidget->addWidget(new QWidget(this));
	mRouteMap.insert(nodeKey, nodeIndex);
	return nodeKey;
}

QString QQClientView::addFooterNode(QWidget* page, int keyPoints, QIcon icon)
{
	QString nodeKey = mNavigationBar->addFooterNode(page, keyPoints, icon);
	int nodeIndex = mCenterStackedWidget->addWidget(page);
	mRouteMap.insert(nodeKey, nodeIndex);
	return nodeKey;
}

void QQClientView::setNodeKeyPoints(QString nodeKey, int keyPoints)
{
	mNavigationBar->setNodeKeyPoints(nodeKey, keyPoints);
}

int QQClientView::getNodeKeyPoints(QString nodeKey) const
{
	return mNavigationBar->getNodeKeyPoints(nodeKey);
}

void QQClientView::setWindowButtonFlag(ElaAppBarType::ButtonType buttonFlag, bool isEnable)
{
	mAppBar->setWindowButtonFlag(buttonFlag, isEnable);
}

void QQClientView::setWindowButtonFlags(ElaAppBarType::ButtonFlags buttonFlags)
{
	mAppBar->setWindowButtonFlags(buttonFlags);
}

ElaAppBarType::ButtonFlags QQClientView::getWindowButtonFlags() const
{
	return mAppBar->getWindowButtonFlags();
}

void QQClientView::showForceOutLineWindow()
{
}

void QQClientView::closeWindow()
{
	mAppBar->closeWindow();
}

void QQClientView::onNavigationNodeClicked(QString nodeKey)
{
	int nodeIndex = mRouteMap.value(nodeKey);
	QTimer::singleShot(180, this, [=]() {
		QWidget* currentWidget = mCenterStackedWidget->widget(nodeIndex);
		mCenterStackedWidget->setCurrentIndex(nodeIndex);
		QPropertyAnimation* currentWidgetAnimation = new QPropertyAnimation(currentWidget, "pos");
		currentWidgetAnimation->setEasingCurve(QEasingCurve::OutCubic);
		currentWidgetAnimation->setDuration(300);
		QPoint currentWidgetPos = currentWidget->pos();
		currentWidgetAnimation->setEndValue(currentWidgetPos);
		currentWidgetPos.setY(currentWidgetPos.y() + 80);
		currentWidgetAnimation->setStartValue(currentWidgetPos);
		currentWidgetAnimation->start(QAbstractAnimation::DeleteWhenStopped);
		});
}
